package com.example.li.day9_19;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Calendar;

public class MyView extends View {
    private static final String TAG = "MyView";
    private String brandText;
    private float brandTextSize;
    private int brandTextColor;
    private int hourHandColor;
    private int miunterHandColor;
    private int secondHandColor;
    private int frameWidth;
    private Paint paint;
    private Paint textPaint;

    public MyView(Context context) {
        super(context);
        Log.d(TAG, "MyView: 1111111111111");
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "MyView:两个参数 ");
        into(attrs);
    }

    private void into(AttributeSet attrs){
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyView);
        brandText = typedArray.getString(R.styleable.MyView_brandText);
        brandTextSize = typedArray.getDimension(R.styleable.MyView_brandTextSize, 0);
        brandTextColor = typedArray.getColor(R.styleable.MyView_brandTextColor, Color.BLACK);

        hourHandColor = typedArray.getColor(R.styleable.MyView_hourHandColor, Color.BLACK);
        miunterHandColor = typedArray.getColor(R.styleable.MyView_miunterHandColor, Color.BLACK);
        secondHandColor = typedArray.getColor(R.styleable.MyView_secondHandColor, Color.BLACK);
        typedArray.recycle();
        intoPanint();
    }

    private void intoPanint(){
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);

        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int specModeWidth = MeasureSpec.getMode(widthMeasureSpec);//得到宽度的布局模式
        int specSizeWidth = MeasureSpec.getSize(widthMeasureSpec);//得到布局参数的宽度
        int specWidth = 0;
        switch (specModeWidth) {
            case MeasureSpec.EXACTLY:
                specWidth = specSizeWidth;
                break;
            case MeasureSpec.AT_MOST:
                specWidth = 100;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }

        int specModeHeight = MeasureSpec.getMode(heightMeasureSpec);//得到高度的布局模式
        int specSizeHeight = MeasureSpec.getSize(heightMeasureSpec);//得到布局参数的高度
        int specHeight = 0;
        switch (specModeHeight) {
            case MeasureSpec.EXACTLY:
                specHeight = specSizeHeight;
                break;
            case MeasureSpec.AT_MOST:
                specHeight = 100;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }
        setMeasuredDimension(specWidth, specHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        calculate();
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        int centerX = measuredWidth / 2;
        int centerY = measuredHeight / 2;
        int size = Math.min(measuredWidth, measuredHeight);//取2个数的最小值
        //时钟边框的粗细
        frameWidth = 20;
        int r = size / 2 - frameWidth / 2;
        paint.setStrokeWidth(frameWidth);

        canvas.drawCircle(centerX, centerY, r, paint);
        canvas.save();

        paint.setStrokeWidth(frameWidth/5);
        canvas.rotate(hourDegrees,centerX,centerY);
        paint.setColor(hourHandColor);
        canvas.drawLine(centerX,centerY,centerX,centerY-r*4/5,paint);

        paint.setStrokeWidth(frameWidth/4);
        canvas.rotate(miunteDegrees,centerX,centerY);
        paint.setColor(miunterHandColor);
        canvas.drawLine(centerX,centerY,centerX,centerY-r*3/5,paint);

        paint.setStrokeWidth(frameWidth/3);
        canvas.rotate(secondDegrees,centerX,centerY);
        paint.setColor(secondHandColor);
        canvas.drawLine(centerX,centerY,centerX,centerY-r*2/5,paint);

        canvas.restore();

        for (int i = 1; i <=12; i++) {
            canvas.save();
            canvas.rotate(30*i,centerX,centerY);
            textPaint.setTextSize(25);
            canvas.drawText(i+"",centerX,centerY-r+30,textPaint);
            canvas.restore();
        }
    }

    private void calculate(){
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR);
        int miunte = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        secondDegrees=second*6;
        miunteDegrees=miunte*6+second/60.0f;
        hourDegrees=hour*30+miunte/12;
    }

    private float hourDegrees;
    private float miunteDegrees;
    private float secondDegrees;
}
